package com.itboye.ihomebank.activity.finance.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.HaveInhaAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.HaveInBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.finance.MyTouZiActivity;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by itboye on 2017/2/16.
 */

public class YiHuiKuanFragment extends BaseFragment implements Observer {
    private int type;
    private ListView listView;
    private HaveInhaAdapter adapter;
    private HaveInBean list;
    private JinRongPresenter presenter;
    String uid;
    int pageSize = 10;
    int pageNum = 1;

    PtrFrameLayout commend_anchor_ptr;//刷新
    private LinearLayout linNull;

    @Override
    public int initView() {
        return R.layout.fragment_haveinhand;
    }

    @Override
    public void onMyActivityCreated() {

        list = new HaveInBean();
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        presenter = new JinRongPresenter(this);
        uid = (String) SPUtils.get(getActivity(), null, SPContants.USER_ID, "");
        presenter.onMyTouZhi(uid, pageNum + "", pageSize + "","3");
        showProgressDialog("数据加载中...",true);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageNum++;
                presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", "3");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onMyTouZhi(uid, pageNum + "", pageSize + "", "3");
            }
        });
        commend_anchor_ptr.autoRefresh();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent Intet = new Intent(getActivity(), MyTouZiActivity.class);
                Intet.putExtra("id", list.getInfo().get(position).getId());
                startActivity(Intet);
            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);

        if (entity != null) {
            if (!entity.getCode().equals(0)) {
                if (entity.getEventType() == JinRongPresenter.my_tou_zi_success) {
                    commend_anchor_ptr.refreshComplete();//设置刷新完成
                    list = (HaveInBean) entity.getData();
                    Log.d("BY_P2p_myInvset", list.getInfo().size() + "");
                    if (list.getInfo().size() <= 0 || list.getInfo() == null) {
                        linNull.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                    } else {
                        linNull.setVisibility(View.GONE);
                        listView.setVisibility(View.VISIBLE);
//                        adapter = new HaveInhaAdapter(list, getActivity());
//                        listView.setAdapter(adapter);
                    }
                    try {
                        closeProgressDialog();
                    }catch (Exception e){}

                }
            }
        }

    }
}
